//操作成功之后提示
function success(text){
	  $("#alerts").html("<div  style='text-align: center; display:no;' class='alert alert-success alert-dismissable'><strong>"+text+"</strong></div>");
	  $("#menus").click();
 
}
//操作失败之后提示

function fail(text){
	  $("#alerts").html("<div  style='text-align: center; display:no;' class='alert alert-success alert-dismissable'><strong>"+text+"</strong></div>");
	  $("#menus").click();

}
function myfresh(){
	window.location.href=window.location.href;
}	

//跳转到不同的订单
function clickOrders(currentPage,type,status){ //  1 1  4
	// type= 0测量 1安装 2维修  // status 4 ,0 ,1 ,5 (6,2)
	if(type==0){
		window.location.href="../orders/toMeasure.do?currentPage="+currentPage+"&type="+type+"&status="+status;
	}else if(type==2){
		window.location.href="../orders/toRepair.do?currentPage="+currentPage+"&type="+type+"&status="+status;
	}else if(type==1){
		window.location.href="../orders/toInstall.do?currentPage="+currentPage+"&type="+type+"&status="+status;
	}
}

//付款
function payIndent(indentId,type){
	$.post(
			"../orders/getAllBillDetailByIndentId.do",
			{indentId:indentId},
			function(data){
					//在模态框中显示所有详细的订单信息
					//订单号
					$("#indentIdDiv").html(data.indentId);
					//用户姓名
					$("#indentName").html(data.name);
					//订单费用
					$("#indentPrice").html(data.merchantsPrice+"元");
					//悬赏费用
					$("#indentAddPrice").html(data.addPrice+"元");
					//总费用
					$("#indentAllPrice").html((data.merchantsPrice+data.addPrice)+"元");
					$("#paySelect").empty();
					$("#paySelect").next().remove();
					$.post(
							"../orders/getAllDataById.do",
							function(data3){
								if(data3.wallet>=(data.merchantsPrice+data.addPrice)){
									//钱包钱够, 就直接用钱包钱																
									$("#paySelect").append(makeRadioForPay("payMethod",0,"账户余额("+data3.wallet+"元)","checked='checked'",""));			
							        var secDiv=$('<div class="form-group" style="position: relative;margin-top: 30px;margin-left: 162px;"></div>');
							        secDiv.append('<label class="col-md-3 col-sm-3 control-label" style="position: relative;margin-top: -10px;">支付密码:</label>');
									secDiv.append('<div class="col-md-4"><input type="password" name="password"  class="form-control"></div>');
									$("#paySelect").after(secDiv);
								}else{
									//钱包钱不够, 那么就适用第三方   disabled="disabled"	
									$("#paySelect").append(makeRadioForPay("payMethod",0,"账户余额("+data3.wallet+"元)","","disabled='disabled'"));
									$("#paySelect").append(makeRadioForPay("payMethod",1,"支付宝","checked='checked'",""));
								}
								
							},
							"json"
					)
					
				
			},
			"json"
	);
	
	
}
//做radio
function makeRadioForPay(name,value,content,checked,disabled){
	var div=$('<div class="radio-inline"></div>');
	var lableInput=$('<label><input type="radio" '+disabled+' name="'+name+'" value="'+value+'" '+checked+' >'+content+'</lable>');
	return div.append(lableInput);
}


//取消订单       0   恢复  其他 取消
function cancel(indentId,status){
	$.post(
			"../orders/cancleIndent.do",
			{indentId:indentId,
			status:status	
			},
			function(data){
				if(data==1){
					success("取消成功!");
					setTimeout(myfresh,1000);
				}else{
					fail("未知原因,取消失败");
					setTimeout(myfresh,1000);
				}
			},
			"json"
	);
} 

//加价
function addPrice(indentId){
	$("#addPrice").find('input[name="indentId"]').first().val(indentId);
}
//保存加价
function saveAddPrice(object){
	var div=$(object).parent().prev();
	var indentId=div.find('input[name="indentId"]').first().val();
	var addPrice=div.find('input[name="addPrice"]').first().val();
	//TODO 调用支付宝付钱, 付钱成功之后
//	$.post(
//			"../orders/saveAddPrice.do",
//			{indentId:indentId,
//			addPrice:addPrice
//			},
//			function(data){
//				if(data==1){
//					success("悬赏成功!");
//					setTimeout(myfresh,1000);
//				}
//			},
//			"json"
//
//	);
	window.open("../orders/saveAddPrice.do?indentId="+indentId+"&addPrice="+addPrice);
	
}
//去支付
function toPay(){
	var context=$("#zhifuForm").serialize();
	context=JSON.parse(updateString(context));
	console.log(context.password);
	if(context.payMethod==0){
		//账户余额支付
		$.post(
				"../orders/toPayOrders.do",
				{
					indentId:$('#indentIdDiv').html(),
					password:context.password
				},
				function(data){
					if(data==1){
						$("#payIndent").modal("hide");
						success("支付成功!");
						setTimeout(myfresh,2000);
					}else if(data==0){
						$("#payIndent").modal("hide");
						fail("密码错误!");
						setTimeout(myfresh,2000);
					}else{
						$("#payIndent").modal("hide");
						fail("服务器出现异常错误, 请与管理员联系!");
						setTimeout(myfresh,2000);
					}
				},
				"json"
		)
		
	}else if(context.payMethod==1){
		//支付宝支付
		//window.location.href="../orders/toPayIndent.do?indentId="+$('#indentIdDiv').html();
		window.open("../orders/toPayIndent.do?indentId="+$('#indentIdDiv').html());
	}else{
		//其他支付
	}
	
}
//根据订单id获取它的所有详细信息 这是修改的
function indentDetailById(indentId){
	$.post(
			"../orders/getAllBillDetailByIndentId.do",
			{indentId:indentId},
			function(data){
				if(data.relievedBao==0){
					var rootDiv=$("#indentDetailById").find('.modal-body').first();			
					var two=$('<div class="form-group" style="position: relative;margin-top: 228px;"></div>');
					two.append('<label class="col-sm-2 control-label">鹿客安心宝:</label>');
					var three=$('<div id="anxinbao" style="position: relative; margin-left: 215px;margin-top:0px"></div>');
					three.append('<div class="checkbox-inline"><label><input type="checkbox" name="relievedBao" value="1" checked="checked"></label></div>');
					rootDiv.append(two);
					rootDiv.append(three);
				}
				$("#indentDetailById").find('input[name="indentId"]').first().val(indentId);
				$("#indentDetailById").find('input[name="name"]').first().val(data.name);
				$("#indentDetailById").find('input[name="phone"]').first().val(data.phone);
				$("#indentDetailById").find('input[name="address"]').first().val(data.address);	
				$("#indentDetailById").find('input[name="cuAdId"]').first().val(data.cuAdId);		
				$("#indentDetailById").find('input[name="quHAddress"]').first().val(data.quHAddress);	
			},
			"json"
	);
}
//保存
function updateIndent(){
	var indentId=$("#indentDetailById").find('input[name="indentId"]').first().val();
	var name=$("#indentDetailById").find('input[name="name"]').first().val();
	var phone=$("#indentDetailById").find('input[name="phone"]').first().val();
	var address=$("#indentDetailById").find('input[name="address"]').first().val();	
	var cuAdId=$("#indentDetailById").find('input[name="cuAdId"]').first().val();		
	var quHAddress=$("#indentDetailById").find('input[name="quHAddress"]').first().val();
	var relievedBao=1;
	if($("#indentDetailById").find('input[name="relievedBao"]:checked')>0){
		relievedBao=$("#indentDetailById").find('input[name="relievedBao"]:checked').val();
	}
	$.post(
			"../orders/updateIndentByInstall.do",
			{
				indentId:indentId,
				name:name,
				phone:phone,
				address:address,
				cuAdId:cuAdId,
				quHAddress:quHAddress,
				relievedBao:relievedBao
			},
			function(data){
				if(data==1){
					success("订单修改成功!");
					
				}
			}
	);
	
}
